package com.ecommerce.model;

import java.util.Date;
import java.util.List;

public class Order {
    private int id;
    private String orderNumber;
    private int userId;
    private String username;
    private double totalAmount;
    private String status;
    private String shippingAddress;
    private Date createdAt;
    private Date updatedAt;
    private List<OrderItem> orderItems;

    public Order() {}

    public Order(int userId, double totalAmount, String shippingAddress) {
        this.userId = userId;
        this.totalAmount = totalAmount;
        this.shippingAddress = shippingAddress;
        this.orderNumber = generateOrderNumber();
    }

    private String generateOrderNumber() {
        return "ORD" + System.currentTimeMillis();
    }

    // Getters and Setters
    public int getId() { return id; }
    public void setId(int id) { this.id = id; }

    public String getOrderNumber() { return orderNumber; }
    public void setOrderNumber(String orderNumber) { this.orderNumber = orderNumber; }

    public int getUserId() { return userId; }
    public void setUserId(int userId) { this.userId = userId; }

    public String getUsername() { return username; }
    public void setUsername(String username) { this.username = username; }

    public double getTotalAmount() { return totalAmount; }
    public void setTotalAmount(double totalAmount) { this.totalAmount = totalAmount; }

    public String getStatus() { return status; }
    public void setStatus(String status) { this.status = status; }

    public String getShippingAddress() { return shippingAddress; }
    public void setShippingAddress(String shippingAddress) { this.shippingAddress = shippingAddress; }

    public Date getCreatedAt() { return createdAt; }
    public void setCreatedAt(Date createdAt) { this.createdAt = createdAt; }

    public Date getUpdatedAt() { return updatedAt; }
    public void setUpdatedAt(Date updatedAt) { this.updatedAt = updatedAt; }

    public List<OrderItem> getOrderItems() { return orderItems; }
    public void setOrderItems(List<OrderItem> orderItems) { this.orderItems = orderItems; }

    @Override
    public String toString() {
        return String.format("订单号: %s | 金额: ¥%.2f | 状态: %s | 时间: %s",
                orderNumber, totalAmount, getStatusText(), createdAt);
    }

    public String toDetailedString() {
        return String.format(
                "订单号: %s\n用户: %s\n总金额: ¥%.2f\n状态: %s\n收货地址: %s\n创建时间: %s",
                orderNumber, username, totalAmount, getStatusText(), shippingAddress, createdAt
        );
    }

    public String getStatusText() {
        switch (status) {
            case "pending": return "待确认";
            case "confirmed": return "已确认";
            case "shipped": return "已发货";
            case "delivered": return "已送达";
            case "cancelled": return "已取消";
            default: return status;
        }
    }
}
